<?php
namespace App\Services;
use App\Model\ConfigModel;
use App\Model\UserWishLog;
use App\Model\UserEnergy;
use App\Model\WishModel;


class Wish
{
    //浇油算法
	/**
	 * times   递归几次
	 * total   总里程数
	 * due     剩余里程数
	 * type    1自己   2好友
	 * nums    递归到第几次了
	 * wid     心愿ID
	 * total   总里程数
	 * uid	   用户ID
	 * one     签到红油
	 * two     好友赠送的红油
	 */
	public function oiling($times,$due,$type,$nums,$wid,$sell_price,$uid,$one=0,$two=0){
        //预留百分比
        $due_km_ratio = ConfigModel::getValue('due_km_ratio');
        //里程区间
        $due_qj_ratio_start = ConfigModel::getValue('due_qj_ratio_start');
        
        $due_qj_ratio_end = ConfigModel::getValue('due_qj_ratio_end'); 
		//查询一桶油价值
		$oling_cny = ConfigModel::getValue('oiling_cny');
		//计算共浇多少次
		$oling_times = ceil($sell_price/$oling_cny);
		//查询该心愿浇的次数
		$add_wish_times = UserwishLog::where('wid',$wid)->count();
		//用户汽油
		$user_energy = UserEnergy::where('uid',$uid)->first();
		//获取用户心愿
		$user_wish = WishModel::where('id',$wid)->first();
        //累计进程
        $sum = UserWishLog::where('wid',$wid)->sum('nums');
     
        if($oling_times - $add_wish_times <= 1){
            //剩余里程数
            $due_num = $user_wish['total']-$user_wish['in_total'];
            //自己浇
            if($type == 1){
                //扣除签到
                if($user_energy['sys_num'] >= 1){
                    $one += 1;
                    $two += 0;
                    UserEnergy::where('uid',$uid)->decrement('sys_num');
                }else{
                    //扣除好友赠送
                    if($user_energy['oth_num'] >= 1){
                        $one += 0;
                        $two += 1;
                        UserEnergy::where('uid',$uid)->decrement('oth_num');
                    }
                }
                //好友浇
            }else{
                //扣除签到
                if($user_energy['sys_num_two'] >= 1){
                    $one += 1;
                    $two += 0;
                    UserEnergy::where('uid',$uid)->decrement('sys_num_two');
                }else{
                    //扣除好友赠送
                    if($user_energy['oth_num_two'] >= 1){
                        $one += 0;
                        $two += 1;
                        UserEnergy::where('uid',$uid)->decrement('oth_num_two');
                    }
                }
            }
            
            WishModel::where('id',$user_wish['id'])->increment('in_total',$due_num);
            UserWishLog::create([
                'wid' => $user_wish['id'],
                'nums' => $due_num,
                'due' => $user_wish['total'] - $user_wish['in_total'],
                'sum' => $sum + $due_num,
                'ratio' => 1,
            ]);
            $due_num = $user_wish['total'] - $user_wish['in_total'];
        }else{
            //判断里程数
            if($user_wish['in_total'] >= $user_wish['total']*$due_km_ratio){
                if($type == 1){
                    //扣除签到
                    if($user_energy['sys_num'] >= 1){
                        $one += 1;
                        $two += 0;
                        UserEnergy::where('uid',$uid)->decrement('sys_num');
                    }else{
                        //扣除好友赠送
                        if($user_energy['oth_num'] >= 1){
                            $one += 0;
                            $two += 1;
                            UserEnergy::where('uid',$uid)->decrement('oth_num');
                        }
                    }
                }else{
                        //扣除签到
                        if($user_energy['sys_num_two'] >= 1){
                        $one += 1;
                        $two += 0;
                        UserEnergy::where('uid',$uid)->decrement('sys_num_two');
                    }else{
                        //扣除好友赠送
                        if($user_energy['oth_num_two'] >= 1){
                            $one += 0;
                            $two += 1;
                            UserEnergy::where('uid',$uid)->decrement('oth_num_two');
                        }
                    }
                }
                
                WishModel::where('id',$user_wish['id'])->increment('in_total');
                UserWishLog::create([
                    'wid' => $user_wish['id'],
                    'nums' => 1,
                    'due' => $user_wish['total'] - $user_wish['in_total'],
                    'sum' => $sum + 1,
                    'ratio' => 0,
                ]);
                $due_num = $due-1;
            }else{
                if($type == 1){
                    //判断是否还有签到的红油
                    if($user_energy['sys_num'] > 0){
                        $one += 1;
                        //扣除用户签到红油
                        UserEnergy::where('uid',$uid)->decrement('sys_num');
                    }else{
                        $two += 1;
                        //扣除用户好友赠送的红油
                        UserEnergy::where('uid',$uid)->decrement('oth_num');
                    }
                }else{
                    
                    //判断是否还有签到的红油
                    if($user_energy['sys_num_two'] > 0){
                        $one += 1;
                        //扣除用户签到红油
                        UserEnergy::where('uid',$uid)->decrement('sys_num_two');
                    }else{
                        $two += 1;
                        //扣除用户好友赠送的红油
                        UserEnergy::where('uid',$uid)->decrement('oth_num_two');
                    }
                }
                
                //计算是不是最后一次交
                if($add_wish_times +1 >= $oling_times){
                    //本次浇灌数量
                    $oling_num = $due;
                    $oling_ratio = 1;
                }else{
                    $oling_ratio = $this->randFloat($due_qj_ratio_start,$due_qj_ratio_end);
                    $oling_num = bcmul($oling_ratio,$due);
                    
                }
                
                if($oling_num < 1){
                    $oling_num = 1;
                }
                
                //增加心愿 里程数
                WishModel::where('id',$wid)->increment('in_total',$oling_num);
                
                //剩余里程数
                $due_num = $due-$oling_num;
            
                UserWishLog::create([
                    'wid' => $wid,
                    'nums' => $oling_num,
                    'due' => $user_wish['total'] - $user_wish['in_total'],
                    'ratio' => $oling_ratio,
                    'sum' => $sum + $oling_num,
                ]);
            }
        }
		$arr['one'] = $one;
		$arr['two'] = $two;
		$user_wish = WishModel::where('id',$wid)->first();
		//判断是否可以执行完毕
		if($user_wish['total'] - $user_wish['in_total'] > 0){
			if($nums+1 < $times){
				return $this->oiling($times,$due_num,$type,$nums+1,$wid,$sell_price,$uid,$one,$two);
			}else{
				return $arr;
			}
		}else{
			return $arr;
		}
	}

	//随机浮点数
	public function randFloat($min = 0, $max = 1) {
		$rand = $min + mt_rand() / mt_getrandmax() * ($max - $min);
		return floatval(number_format($rand,3));
	}
}